/**
 * 
 */
package pt.mleiria.bo;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import pt.mleiria.dao.Alarm;
import pt.mleiria.room.LivingRoom;
import pt.mleiria.sound.MusicPlay;
import pt.mleiria.sound.TextToSpeech;

/**
 * @author NB13712
 *
 */
@Stateless
public class LivingRoomEJB {
	private static final Logger LOG = Logger.getLogger(LivingRoomEJB.class);
	private LivingRoom lr = LivingRoom.getInstance();
	@PersistenceContext(unitName = "DomusService")
	private EntityManager em;
	
	public void setAlarmWithMusic(final String hhmm, final String pathToMusic){
		lr.setSoundBehavior(new MusicPlay(pathToMusic));
		lr.setAlarm(hhmm);
		
	}
	
	public void setAlarmWithVoice(final String hhmm, final String textVoice){
		lr.setSoundBehavior(new TextToSpeech(textVoice));
		lr.setAlarm(hhmm);
	}
	
	public void powerOffLivingRoom(){
		LOG.info("Power of...");
		LOG.info(lr.powerOff());
	}
	/**
	 * 
	 * @return
	 */
	public Alarm getCurrentAlarm() {
		try {
			Alarm alarm = em.find(Alarm.class, 1);
			return alarm;
		} catch (NoResultException e) {
			return new Alarm();
		}
	}
	public void saveAlarm(final Alarm alarm){
		em.persist(alarm);
		/*
		Query q = em.createQuery("update alarm a where a = :alarm");
		q.setParameter("alarm", alarm);
		*/
	}

}
